﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System
{
    /// <summary>
    /// 自定义返回值
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class RValue<T>
    {
        public RValue()
        {
            this.Success = true;
        }

        public RValue(T value) : this(value, null) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="message"></param>
        public RValue(T value, string message) : this(true, value, message) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="success"></param>
        /// <param name="value"></param>
        /// <param name="message"></param>
        public RValue(bool success, T value, string message) : this(success, value, message, null) { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="success"></param>
        /// <param name="value"></param>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public RValue(bool success, T value, string message, Exception ex)
        {
            this.Success = success;
            this.Value = value;
            this.Message = message;
            this.Exception = ex;
        }

        /// <summary>
        /// 获取或设置是否成功
        /// </summary>
        public bool Success { get; private set; }

        public T Value { get; private set; }

        private string _message;
        /// <summary>
        /// 获取或设置消息文本.
        /// </summary>
        public string Message
        {
            get
            {
                if (_message == null)
                    _message = string.Empty;
                return _message;
            }
            private set
            {
                _message = value;
            }
        }

        private Exception _exception;
        /// <summary>
        /// 获取异常信息
        /// </summary>
        public Exception Exception
        {
            get
            {
                if (this._exception == null && !this.Success)
                    this._exception = new Exception(this.Message);
                return _exception;
            }
            private set { _exception = value; }
        }

    }
}
